﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using saigonbook.Biz;
using System.Text.RegularExpressions;
using System.IO;
using System.Drawing.Imaging;

namespace quanlybansach.Frm
{
    public partial class Frm_quanlysach : Form
    {
        public Frm_quanlysach()
        {
            InitializeComponent();
            btnluuhinhanh.Enabled = false;
            btnsuasach.Enabled = false;
            btnxoasach.Enabled = false;
            dataGridView_Sach.AllowUserToAddRows = false;
            DataGridViewImageColumn dc = dataGridView_Sach.Columns["hinhanh"] as DataGridViewImageColumn;
            dc.ImageLayout = DataGridViewImageCellLayout.Zoom;
        }
        List<BIZ_sach> danhsachsach = new List<BIZ_sach>();

        private void Frm_quanlysach_Load(object sender, EventArgs e)
        {
            danhsachsach = BIZ_sach.Select_sach();
            int stt = 0;
            foreach (BIZ_sach sach in danhsachsach)
            {
                stt++;
                string giabia = sach.giabia.ToString("c");
                giabia = giabia.Substring(1, giabia.LastIndexOf(".") - 1);
                string giaban = sach.giaban.ToString("c");
                giaban = giaban.Substring(1, giaban.LastIndexOf(".") - 1);
                dataGridView_Sach.Rows.Add(stt, sach.masach, sach.tensach, sach.Tenloai, sach.tacgia,
                                            sach.Tennhaxuatban, sach.namxuatban, giabia, giaban,
                                            sach.sotrang, sach.ngaycapnhat.Date.ToShortDateString(), 
                                            sach.HinhAnh, sach.tomtatnoidung);
            }
            cbb_loaisach = BIZ_sach.Cbb_loaisach(cbb_loaisach);
            cbb_nhaxuatban = BIZ_sach.Cbb_NXB(cbb_nhaxuatban);
            txtmasach.Text = BIZ_sach.laymasach().ToString();
        }

        private void txttimkiemsach_KeyPress(object sender, KeyPressEventArgs e)
        {
            dataGridView_Sach.Rows.Clear();
            List<BIZ_sach> ds = new List<BIZ_sach>();
            ds = (from sach in danhsachsach
                  where sach.tensach.ToLower().Contains(txttimkiemsach.Text.ToLower())
                  select sach).ToList();
            int stt = 0;
            foreach (BIZ_sach s in ds)
            {
                stt++;
                string giabia = s.giabia.ToString("c");
                giabia = giabia.Substring(1, giabia.LastIndexOf(".") - 1);
                string giaban = s.giaban.ToString("c");
                giaban = giaban.Substring(1, giaban.LastIndexOf(".") - 1);
                dataGridView_Sach.Rows.Add(stt, s.masach, s.tensach, s.Tenloai, s.tacgia,
                                            s.Tennhaxuatban, s.namxuatban, giabia, giaban,
                                            s.sotrang, s.ngaycapnhat.Date.ToShortDateString(),
                                            s.HinhAnh, s.tomtatnoidung);
            }
        }

        private bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }

        private string catchuoi(string chuoi)
        {
            while (chuoi.Contains("  "))
            {
                chuoi = chuoi.Replace("  ", " ");
            }
            return chuoi;
        }

        private bool kiemtrachuoi(string chuoi)
        {
            if(chuoi.Length >= 4)
                if(chuoi.Substring(chuoi.Length - 4, 4).Equals(".jpg"))
                    return true;
            return false;
        }

        private void btnthemsach_Click(object sender, EventArgs e)
        {
            if ("".Equals(txttensach.Text) || "Chọn loại".Equals(cbb_loaisach.Text) || "".Equals(txttacgia.Text)
                || "Chọn nhà xuất bản".Equals(cbb_nhaxuatban.Text) || "".Equals(txtgiabia.Text) || "".Equals(txtgiaban.Text)
                || "".Equals(txtsotrang.Text) || "".Equals(txthinhanh.Text) || "".Equals(txttomtatnoidung.Text))
                MessageBox.Show("Thông tin chưa đầy đủ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (!IsNumber(txtnamxuatban.Text) || !IsNumber(txtgiabia.Text) || !IsNumber(txtgiaban.Text) || !IsNumber(txtsotrang.Text))
                    MessageBox.Show("'Năm xuất bản', 'Giá', 'Số trang' phải là số!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    BIZ_sach s = new BIZ_sach();
                    s.masach = int.Parse(txtmasach.Text);
                    s.loai = new BIZ_loaisach();
                    s.loai.maloai = int.Parse(cbb_loaisach.SelectedValue.ToString());
                    s.tensach = txttensach.Text.Trim();
                    s.tensach = catchuoi(s.tensach);
                    s.tacgia = txttacgia.Text.Trim();
                    s.tacgia = catchuoi(s.tacgia);
                    s.nhaxuatban = new BIZ_nhaxuatban();
                    s.nhaxuatban.manhaxuatban = int.Parse(cbb_nhaxuatban.SelectedValue.ToString());
                    s.namxuatban = int.Parse(txtnamxuatban.Text);
                    s.giabia = int.Parse(txtgiabia.Text);
                    s.giaban = int.Parse(txtgiaban.Text);
                    s.sotrang = int.Parse(txtsotrang.Text);
                    if (kiemtrachuoi(catchuoi(txthinhanh.Text.Trim())))
                        s.hinhanh = catchuoi(txthinhanh.Text.Trim());
                    else
                        s.hinhanh = catchuoi(txthinhanh.Text.Trim()) + ".jpg";
                    try
                    {
                        string path = @"..\\..\\..\\hethongbansach\hethongbansach\";
                        pictureBox_sach.Image.Save(path + s.hinhanh, ImageFormat.Jpeg);
                        s.ngaycapnhat = Convert.ToDateTime(ngaycapnhat.Text.ToString());
                        s.tomtatnoidung = txttomtatnoidung.Text.Trim();
                        s.tomtatnoidung = catchuoi(s.tomtatnoidung);
                        BIZ_sach.Them_sach(s);
                        MessageBox.Show("Thêm thành công");
                        dataGridView_Sach.Rows.Clear();
                        danhsachsach = BIZ_sach.Select_sach();
                        int stt = 0;
                        foreach (BIZ_sach sach in danhsachsach)
                        {
                            stt++;
                            string giabia = sach.giabia.ToString("c");
                            giabia = giabia.Substring(1, giabia.LastIndexOf(".") - 1);
                            string giaban = sach.giaban.ToString("c");
                            giaban = giaban.Substring(1, giaban.LastIndexOf(".") - 1);
                            dataGridView_Sach.Rows.Add(stt, sach.masach, sach.tensach, sach.Tenloai, sach.tacgia,
                                                        sach.Tennhaxuatban, sach.namxuatban, giabia, giaban,
                                                        sach.sotrang, sach.ngaycapnhat.Date.ToShortDateString(),
                                                        sach.HinhAnh, sach.tomtatnoidung);
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Đường hình ảnh không hợp lệ!");
                    }
                }
            }
        }

        OpenFileDialog open;
        private void btnhinhanh_Click(object sender, EventArgs e)
        {
            open = new OpenFileDialog();
            open.InitialDirectory = ":\\Images";
            open.Filter = "JPG Image(*.jpg)|*.jpg|BMP Image(*.bmp)|*.bmp|PNG Image(*.png)|*.png";
            DialogResult result = open.ShowDialog();
            if (result == DialogResult.OK)
            {
                pictureBox_sach.Image = Image.FromStream(open.OpenFile());
            }
            pictureBox_sach.SizeMode = PictureBoxSizeMode.StretchImage;
            txthinhanh.Text = "image/";
        }

        private void btnluuhinhanh_Click(object sender, EventArgs e)
        {
            if (txthinhanh.Text == "")
                MessageBox.Show("Nhập tên hình ảnh cần save");
            else
            {
                string path = @"..\\..\\..\\hethongbansach\hethongbansach\";
                if (txthinhanh.Text.Contains(".jpg"))
                    pictureBox_sach.Image.Save(path + txthinhanh.Text, ImageFormat.Jpeg);
                else
                    pictureBox_sach.Image.Save(path + txthinhanh.Text + ".jpg", ImageFormat.Jpeg);
                MessageBox.Show("Lưu hình ảnh thành công");
            }
        }

        private void dataGridView_Sach_Click(object sender, DataGridViewCellEventArgs e)
        {
            btnthemsach.Enabled = false;
            btnsuasach.Enabled = true;
            btnxoasach.Enabled = true;
            if (dataGridView_Sach.CurrentCell != null)
            {
                int ma = int.Parse(dataGridView_Sach.CurrentRow.Cells[1].Value.ToString().Trim());
                foreach (BIZ_sach s in danhsachsach)
                {
                    if (s.masach == ma)
                    {
                        txtmasach.Text = s.masach.ToString();
                        txttensach.Text = s.tensach;
                        cbb_loaisach.Text = s.Tenloai;
                        txttacgia.Text = s.tacgia;
                        cbb_nhaxuatban.Text = s.Tennhaxuatban;
                        txtnamxuatban.Text = s.namxuatban.ToString();
                        txtgiabia.Text = s.giabia.ToString();
                        txtgiaban.Text = s.giaban.ToString();
                        txtsotrang.Text = s.sotrang.ToString();
                        txthinhanh.Text = s.hinhanh;
                        pictureBox_sach.Image = s.HinhAnh;
                        pictureBox_sach.SizeMode = PictureBoxSizeMode.StretchImage;
                        txttomtatnoidung.Text = s.tomtatnoidung;
                        break;
                    }
                }
            }
        }

        private void btnsuasach_Click(object sender, EventArgs e)
        {
            if ("".Equals(txttensach.Text) || "Chọn loại".Equals(cbb_loaisach.Text) || "".Equals(txttacgia.Text)
                || "Chọn nhà xuất bản".Equals(cbb_nhaxuatban.Text) || "".Equals(txtgiabia.Text) || "".Equals(txtgiaban.Text)
                || "".Equals(txtsotrang.Text) || "".Equals(txthinhanh.Text) || "".Equals(txttomtatnoidung.Text))
                MessageBox.Show("Thông tin chưa đầy đủ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (!IsNumber(txtnamxuatban.Text) || !IsNumber(txtgiabia.Text) || !IsNumber(txtgiaban.Text) || !IsNumber(txtsotrang.Text))
                    MessageBox.Show("'Năm xuất bản', 'Giá', 'Số trang' phải là số!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    danhsachsach = BIZ_sach.Select_sach();
                    BIZ_sach s = new BIZ_sach();
                    s.masach = int.Parse(txtmasach.Text);
                    s.loai = new BIZ_loaisach();
                    s.loai.maloai = int.Parse(cbb_loaisach.SelectedValue.ToString());
                    s.tensach = txttensach.Text.Trim();
                    s.tensach = catchuoi(s.tensach);
                    s.tacgia = txttacgia.Text.Trim();
                    s.tacgia = catchuoi(s.tacgia);
                    s.nhaxuatban = new BIZ_nhaxuatban();
                    s.nhaxuatban.manhaxuatban = int.Parse(cbb_nhaxuatban.SelectedValue.ToString());
                    s.namxuatban = int.Parse(txtnamxuatban.Text);
                    s.giabia = int.Parse(txtgiabia.Text);
                    s.giaban = int.Parse(txtgiaban.Text);
                    s.sotrang = int.Parse(txtsotrang.Text);
                    s.hinhanh = catchuoi(txthinhanh.Text.Trim());
                    s.ngaycapnhat = Convert.ToDateTime(ngaycapnhat.Text.ToString());
                    s.tomtatnoidung = txttomtatnoidung.Text.Trim();
                    s.tomtatnoidung = catchuoi(s.tomtatnoidung);
                    if (kiemtrachuoi(catchuoi(txthinhanh.Text.Trim())))
                        s.hinhanh = catchuoi(txthinhanh.Text.Trim());
                    else
                        s.hinhanh = catchuoi(txthinhanh.Text.Trim()) + ".jpg";
                    danhsachsach = (from sa in danhsachsach where sa.masach == s.masach select sa).ToList();
                    foreach (BIZ_sach _sach in danhsachsach)
                        if (_sach.masach == s.masach && !_sach.hinhanh.Equals(s.hinhanh))
                        {
                            try
                            {
                                string path = @"..\\..\\..\\hethongbansach\hethongbansach\";
                                pictureBox_sach.Image.Save(path + s.hinhanh, ImageFormat.Jpeg);
                                BIZ_sach.Sua_sach(s);
                                MessageBox.Show("Sửa thành công!");
                                btnthemsach.Enabled = true;
                                cbb_loaisach.Text = "Chọn loại";
                                txttensach.Text = "";
                                txttacgia.Text = "";
                                cbb_nhaxuatban.Text = "Chọn nhà xuất bản";
                                txtnamxuatban.Text = "";
                                txtgiabia.Text = "";
                                txtgiaban.Text = "";
                                txtsotrang.Text = "";
                                txthinhanh.Text = "";
                                txttomtatnoidung.Text = "";
                                pictureBox_sach.Image = null;
                                dataGridView_Sach.Rows.Clear();
                                danhsachsach = BIZ_sach.Select_sach();
                                int stt = 0;
                                foreach (BIZ_sach sach in danhsachsach)
                                {
                                    stt++;
                                    string giabia = sach.giabia.ToString("c");
                                    giabia = giabia.Substring(1, giabia.LastIndexOf(".") - 1);
                                    string giaban = sach.giaban.ToString("c");
                                    giaban = giaban.Substring(1, giaban.LastIndexOf(".") - 1);
                                    dataGridView_Sach.Rows.Add(stt, sach.masach, sach.tensach, sach.Tenloai, sach.tacgia,
                                                                sach.Tennhaxuatban, sach.namxuatban, giabia, giaban,
                                                                sach.sotrang, sach.ngaycapnhat.Date.ToShortDateString(),
                                                                sach.HinhAnh, sach.tomtatnoidung);
                                }
                            }
                            catch
                            {
                                MessageBox.Show("Đường dẫn hình ảnh không hợp lệ");
                            }
                        }
                        else
                        {
                            BIZ_sach.Sua_sach(s);
                            MessageBox.Show("Sửa thành công!");
                            btnthemsach.Enabled = true;
                            cbb_loaisach.Text = "Chọn loại";
                            txttensach.Text = "";
                            txttacgia.Text = "";
                            cbb_nhaxuatban.Text = "Chọn nhà xuất bản";
                            txtnamxuatban.Text = "";
                            txtgiabia.Text = "";
                            txtgiaban.Text = "";
                            txtsotrang.Text = "";
                            txthinhanh.Text = "";
                            txttomtatnoidung.Text = "";
                            pictureBox_sach.Image = null;
                            dataGridView_Sach.Rows.Clear();
                            danhsachsach = BIZ_sach.Select_sach();
                            int stt = 0;
                            foreach (BIZ_sach sach in danhsachsach)
                            {
                                stt++;
                                string giabia = sach.giabia.ToString("c");
                                giabia = giabia.Substring(1, giabia.LastIndexOf(".") - 1);
                                string giaban = sach.giaban.ToString("c");
                                giaban = giaban.Substring(1, giaban.LastIndexOf(".") - 1);
                                dataGridView_Sach.Rows.Add(stt, sach.masach, sach.tensach, sach.Tenloai, sach.tacgia,
                                                            sach.Tennhaxuatban, sach.namxuatban, giabia, giaban,
                                                            sach.sotrang, sach.ngaycapnhat.Date.ToShortDateString(),
                                                            sach.HinhAnh, sach.tomtatnoidung);
                            }
                        }
                }
            }
        }

        private void btnxoasach_Click(object sender, EventArgs e)
        {
            BIZ_sach s = new BIZ_sach();
            s.masach = int.Parse(txtmasach.Text);
            if (!BIZ_sach.kiemtraxoa(s))
            {
                MessageBox.Show("Không được xóa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (MessageBox.Show("Bạn có muốn xóa sách " + s.tensach + " có mã là " + s.masach + " không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    BIZ_sach.Xoa_sach(s);
                    MessageBox.Show("Xóa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btnthemsach.Enabled = true;
                    txttensach.Text = "";
                    txttacgia.Text = "";
                    txtnamxuatban.Text = "";
                    txtgiabia.Text = "";
                    txtgiaban.Text = "";
                    txtsotrang.Text = "";
                    txthinhanh.Text = "";
                    txttomtatnoidung.Text = "";
                    pictureBox_sach.Image = null;
                    danhsachsach = BIZ_sach.Select_sach();
                    int stt = 0;
                    foreach (BIZ_sach sach in danhsachsach)
                    {
                        stt++;
                        string giabia = sach.giabia.ToString("c");
                        giabia = giabia.Substring(1, giabia.LastIndexOf(".") - 1);
                        string giaban = sach.giaban.ToString("c");
                        giaban = giaban.Substring(1, giaban.LastIndexOf(".") - 1);
                        dataGridView_Sach.Rows.Add(stt, sach.masach, sach.tensach, sach.Tenloai, sach.tacgia,
                                                    sach.Tennhaxuatban, sach.namxuatban, giabia, giaban,
                                                    sach.sotrang, sach.ngaycapnhat.Date.ToShortDateString(),
                                                    sach.HinhAnh, sach.tomtatnoidung);
                    }
                }
            }
        }

        private void btnlammoi_Click(object sender, EventArgs e)
        {
            BIZ_sach s = new BIZ_sach();
            cbb_loaisach = BIZ_sach.Cbb_loaisach(cbb_loaisach);
            cbb_nhaxuatban = BIZ_sach.Cbb_NXB(cbb_nhaxuatban);
            int count = dataGridView_Sach.RowCount + 1;
            txtmasach.Text = BIZ_sach.laymasach().ToString();
            btnthemsach.Enabled = true;
            txttensach.Text = "";
            txttacgia.Text = "";
            txtnamxuatban.Text = "";
            txtgiabia.Text = "";
            txtgiaban.Text = "";
            txtsotrang.Text = "";
            txthinhanh.Text = "";
            txttomtatnoidung.Text = "";
            pictureBox_sach.Image = null;
        }

        private void btndong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
